/*
 *  Author: Neil Wicker
 *  Date: 29/04/2014
 *  Description: GameObject class, used as a base class for on-screen objects.
 *				 
 *	Includes standard object members such as dimensions, bitmap and colour.
 */

package com.forbiddendesert.logic;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;

public class GameObject 
{
	protected Bitmap mBitmap;
	protected Paint mPaint;
	private Rect mSource;
	private Rect mDestination;
	
	// The colour to use
	protected int[] mColour;
	
	// Dimensions
	protected int mX;
	protected int mY;
	protected int mWidth;
	protected int mHeight;
	protected int mHalfWidth;
	protected int mHalfHeight;
	
	public GameObject(Context context)
	{
		// Initialise bitmap, paint and rectangles
		mBitmap = null;
		mPaint = new Paint();
		mSource = new Rect();
		mDestination = new Rect();
		
		// The default bitmap colour
		mColour = new int[4];
		mColour[0] = 255;
		mColour[1] = 0;
		mColour[2] = 255;
		mColour[3] = 255;
		
		// Default scaling, can be changed in sub-classes
		mX = 100;
		mY = 100;
		mWidth 	= 100;
		mHeight = 100;
		mHalfWidth 	= 50;
		mHalfHeight = 50;
	}
	
	public GameObject()
	{
		
	}
	
	protected Bitmap loadBitmap(Resources Res, int id)
	{
		// Load the object bitmap
		return BitmapFactory.decodeResource(Res,id);
	}
	
	protected void setPaint()
    {		
		// Set the colour matrix
		ColorMatrix colourMatrix = new ColorMatrix();
		colourMatrix.set(new float[]  
        {
				mColour[0], 0, 0, 0, 0,
                0, mColour[1], 0, 0, 0,
                0, 0, mColour[2], 0, 0,
                0, 0, 0, mColour[3], 0,
                0, 0, 0, 0, 1
        }); 
 
		ColorMatrixColorFilter colourFilter = new ColorMatrixColorFilter(colourMatrix); 
		// Set the paint using this colour matrix
        mPaint.setColorFilter(colourFilter);   
    }
 
	public void setPosition(int x, int y)
	{
		// Update the position
		mX = x;
		mY = y;
		updateDestinationRectangle();
	}

	public void slide(int newX, int newY)
	{
		
	}
	
	// Set the bitmap 
	protected void updateSourceRectangle()
	{
		mSource.set(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
	}
	
	// Set the position
	protected void updateDestinationRectangle()
	{
		mDestination.set(mX - mHalfWidth, mY - mHalfHeight, mX + mHalfWidth, mY + mHalfHeight);
	}
	
	public void draw(Canvas canvas)
	{
		// Draw the final bitmap
		canvas.drawBitmap(mBitmap, mSource, mDestination, mPaint);
	}
	
}
